package com.roger.myHome.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.roger.myHome.domain.TbAssets;
import com.roger.myHome.entity.dto.AssetsUpdateReq;
import com.roger.myHome.entity.qto.AssetsQry;
import com.roger.myHome.mapper.AssetsMapper;
import com.roger.myHome.page.EUDataGridResult;
import com.roger.myHome.service.AssetsService;
import com.roger.myHome.util.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;

@Service
public class AssetsServiceImpl implements AssetsService {
    @Resource
    private AssetsMapper assetsMapper;

    @Override
    public EUDataGridResult page(AssetsQry qryReq) {
        return new EUDataGridResult(assetsMapper.page(qryReq), assetsMapper.totalSize(qryReq));
    }

    @Override
    public String update(AssetsUpdateReq req) {
        LambdaUpdateWrapper<TbAssets> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(TbAssets::getId, req.getId())
                .set(TbAssets::getMoney, req.getMoney())
                .set(TbAssets::getLastedUpdateTime, (new Date()));
        assetsMapper.update(null, lambdaUpdateWrapper);

        Map hashMap = new HashMap();
        hashMap.put("result", "修改成功!");
        return StringUtils.getJSONString(hashMap);
    }

    @Override
    public String searchAll() {
        Map<String, String> hashMap = new HashMap();
        hashMap.put("result", "总资产:" + assetsMapper.searchAll());
        return StringUtils.getJSONString(hashMap);
    }

    @Override
    public String searchAllCash() {
        Map<String, String> hashMap = new HashMap();
        hashMap.put("result", "总资产:" + assetsMapper.searchAllCash());
        return StringUtils.getJSONString(hashMap);
    }
}
